<?php

namespace App\Console\Commands;

use App\Helpers\Helpers;
use App\Model\Article;
use App\Model\OrderPayRecord;
use App\Model\UserAuthTag;
use App\Model\UserPoint;
use App\Model\UserRole;
use App\Model\WineCoupon;
use App\Model\WineProduct;
use App\Model\WineProductDealerRelation;
use App\Model\WineProductImporterRelation;
use App\Model\WineProductScene;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 't:run {type} {--table=} {--password=} {--userid=} {--reset=} {--point=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->argument('type');
        $arr = get_class_methods(__CLASS__);
        $this->log('run command_'.$type);

        if(in_array('command_'.$type,$arr)) {
            $cmd = 'command_'.$type;
            $this->line(date('Y-m-d H:i:s').' run: '.$cmd);
            $this->$cmd();
            $this->line('done');
            $this->log('finished command_'.$type);
        }else{
            $this->command_notice();
            $this->line('not has '.$type.' command');
        }
    }

    //日志存放在job_command_log
    private function log($data)
    {
        Helpers::log($data,'test_command_log');
    }

    public function command_notice()
    {
        $this->line('1.notice          >       notice');
    }

    private function command_update()
    {

    }

    private function command_create_super()
    {
        $u = new User();
        $u->name = "super";
        $u->account = "super";
        $u->userType = "super";
        $u->state = 1;
        $u->password = bcrypt('keentech@2018');
        $u->save();
    }
    private function command_create_admin()
    {
        $u = new User();
        $u->name = "admin";
        $u->account = "admin";
        $u->userType = "admin";
        $u->state = 1;
        $u->password = bcrypt('keentech@2018');
        $u->save();
    }
    private function command_all_user_password_rest()
    {
        $password = $this->option('password');
        if (!$password) $password = 888888;
        $rows = User::all();
        foreach ($rows as $row) {
            $row->password = bcrypt($password);
            $row->save();
        }
    }

    private function command_db()
    {
        $path = base_path().'/db/'. date('Ymd_His').'.sql';
        $cmd = 'mysqldump -u'.env('DB_USERNAME').' -p'.env('DB_PASSWORD').' '.env('DB_DATABASE').'>'.$path;

        exec($cmd);
    }

    private function command_t()
    {
        foreach (User::all() as $user){
            if($user->parent){
                $user->parent_name = $user->parent->name;
                $user->save();
            }
        }
    }


    private function command_seed()
    {
        $userid = explode(',',env('DEV_USER_ID'));
        $point = $this->option('point');
        $reset = $this->option('reset');

        if (!$point) {
            $this->error('please input point!');
        }
        foreach ($userid as $id)
        {
            $u = User::find($id);

            if ($u){

                $m = new UserPoint();
                $m->user_id = $id;
                $m->obj_id = 0;
                $m->obj_type = "admin";
                $m->objTypeName = "后台分配";
                $m->desc = "后台分配用于测试";
                $m->point = $point;
                if ($reset=="true"){
                    $m->desc = "清空后台分配";
                    $m->point = -1*$u->point;
                }

                $m->save();

                User::updateUserPoint($id);
            }

        }
    }

}
